package com.bicon.botu.mall.base.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.base.common.ValidationUtil;
import com.bicon.botu.mall.base.dto.DeliveryPointDto;
import com.bicon.botu.mall.base.entity.DeliveryPoint;
import com.bicon.botu.mall.base.service.IDeliveryPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 发货点 前端控制器
 *
 * @author liushujie
 * @date 2017/7/19 下午 14:49
 */
@RestController
@CrossOrigin
@RequestMapping("/V3.0.0/delivery-point")
public class DeliveryPointController extends BaseController {

    @Autowired
    IDeliveryPointService iDeliveryPointService;
    /**
     * 添加收货点
     * @param tenantId 商户ID
     * @param name 收货点名称
     * @param shipper 收货人
     * @param region 区域
     * @param address 详细地址
     * @param telephone 联系方式
     * @return boolean true、false
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult insert(@RequestParam("tenantId") String tenantId,
                             @RequestParam("name") String name,
                             @RequestParam("shipper") String shipper,
                             @RequestParam("region") String region,
                             @RequestParam("address") String address,
                             @RequestParam("telephone") String telephone) {
        if(ValidationUtil.isEmpty(name)){
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        if(ValidationUtil.isEmpty(shipper)){
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        if(ValidationUtil.isEmpty(region)){
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        if(ValidationUtil.isEmpty(address)){
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        if(ValidationUtil.isEmpty(telephone)){
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        return iDeliveryPointService.insert(tenantId, name, shipper,region,address,telephone);
    }


    /**
     * 初始化所有发货点到redis
     * @return
     */
    @RequestMapping(value = "/init-redis", method = RequestMethod.POST)
    public BaseResult initDeliveryToRedis() {
        return iDeliveryPointService.initDeliveryPointToRedis();
    }

    /**
     * 修改发货点
     * @param id 发货点ID
     * @param tenantId 商户ID
     * @param name 收货点名称
     * @param shipper 收货人
     * @param region 区域
     * @param address 详细地址
     * @param telephone 联系方式
     * @return boolean true、false
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult update(@RequestParam("id") String id,@RequestParam("tenantId") String tenantId,
                             @RequestParam(value = "name",required = false) String name,
                             @RequestParam(value = "shipper", required = false) String shipper,
                             @RequestParam(value = "region", required = false) String region,
                             @RequestParam(value = "address", required = false) String address,
                             @RequestParam(value = "telephone", required = false) String telephone) {
        return iDeliveryPointService.updateDeliveryPoint(id,tenantId, name, shipper,  region,  address,  telephone);
    }

    /**
     * 逻辑删除发货点[批量]
     * @param ids 频道Id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult deleteDeliveryPoint(@RequestParam("ids") String ids) {
        return iDeliveryPointService.deleteDeliveryPoint(ids);
    }


    /**
     * 设置为默认发货点
     * @param id 发货点ID
     * @return
     */
    @RequestMapping(value = "/set-default-delivery", method = RequestMethod.POST)
    public BaseResult setDealutDelivery(@RequestParam("id") String id,@RequestParam("state") Integer state) {
        return iDeliveryPointService.setDealutDelivery(id,state);
    }

    /**
     * 设置为默认收货点
     * @param id 收货点ID
     * @return
     */
    @RequestMapping(value = "/set-default-receive", method = RequestMethod.POST)
    public BaseResult setDealutReceive(@RequestParam("id") String id,@RequestParam("state") Integer state) {
        return iDeliveryPointService.setDealutReceive(id,state);
    }


    /**
     * 根据发货点ID获取发货点详情
     * @param id 发货点ID
     * @return 发货点对象
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public BaseResult detail(@RequestParam("id") String id) {
        DeliveryPointDto deliveryPointDto = iDeliveryPointService.details(id);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, deliveryPointDto);
    }


    /**
     * 发货点列表
     * @param pageNo 当前页
     * @param pageSize 页码
     * @param tenantId 商户id
     * @return 发货点列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public BaseResult select(@RequestParam int pageNo, @RequestParam int pageSize,@RequestParam(value = "tenantId", required = false) String tenantId) {
        Page<DeliveryPoint> deliveryPointPage = null;
        if(ValidationUtil.isEmpty(tenantId)){
            deliveryPointPage = new DeliveryPoint().selectPage(new Page<>(pageNo, pageSize), new EntityWrapper<DeliveryPoint>().eq("logic_delete", Constants.DEL_LOGIC_NORMAL).orderBy("is_delivery",false).orderBy("is_receive",false).orderBy("create_date", false));
        }else{
            deliveryPointPage = new DeliveryPoint().selectPage(new Page<>(pageNo, pageSize), new EntityWrapper<DeliveryPoint>().eq("tenant_id", tenantId).eq("logic_delete", Constants.DEL_LOGIC_NORMAL).orderBy("is_delivery",false).orderBy("is_receive",false).orderBy("update_date", false));
        }
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, deliveryPointPage);
    }


    /**
     * 获取发货点列表，按照默认发货排序
     * @return
     */
    @RequestMapping(value = "/delivery-list", method = RequestMethod.GET)
    public BaseResult enabled() {
        List<DeliveryPoint> deliveryPointList = new DeliveryPoint().selectList(new EntityWrapper<DeliveryPoint>().eq("logic_delete", Constants.DEL_LOGIC_NORMAL).orderBy("is_delivery", false).orderBy("create_date", false));
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, deliveryPointList);
    }

    /***
     * 获取收货列表，按照默认排序
     * @return
     */
    @RequestMapping(value = "/receive-list", method = RequestMethod.GET)
    public BaseResult list() {
        //根据默认收货 创建时间 排序
        List<DeliveryPoint> deliveryPointList = new DeliveryPoint().selectList(new EntityWrapper<DeliveryPoint>().eq("logic_delete", Constants.DEL_LOGIC_NORMAL).orderBy("is_receive", false).orderBy("create_date", false));
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, deliveryPointList);
    }

}
